﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace FastKnow.Cache
{
    public class AspnetCache : ICache
    {
        string _name;
        int _count;
        static Dictionary<string, Object> _cache;

        #region ICache 成员

        public void Init()
        {
            throw new NotImplementedException();
        }

        public string Name
        {
            get { return _name; }
        }

        public object this[string key]
        {
            get { return this.Get(key); }
            set { this.Add(key, value); }
        }

        public object this[string key, ICacheDependency cacheDependency]
        {
            set { this.Add(key, value, cacheDependency); }
        }

        public int Count
        {
            get { return _cache.Count; }
        }

        public void Add(string key, object value, ICacheDependency cacheDependency)
        {
            _cache.Add(key, new CacheItem(key, value, cacheDependency));
        }

        public void Add(string key, object value)
        {
            _cache.Add(key, new CacheItem(key, value, new NullCacheDependency()));
        }

        public object Get(string key)
        {
            object @value = null;
            if (_cache.TryGetValue(key, out @value))
                return @value;
            else
                return null;

        }

        public void Remove(string key)
        {
            _cache.Remove(key);
        }

        public bool Contains(string key)
        {
            return _cache.ContainsKey(key);
        }

        public void Clear()
        {
            _cache.Clear();
        }

        #endregion
    }
}
